//
//  ResultViewController.m
//  CardWarrior
//
//  Created by Tekka Xie on 12-4-30.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import "ResultViewController.h"
#import "AudioManager.h"
#import "GameDataManager.h"
#import "StringSlicer.h"
#import "FontManager.h"
#import "EasyGlyph.h"

@implementation ResultViewController

@synthesize maskView;
@synthesize playAgainBtn;
@synthesize backToMenuBtn;
@synthesize selectCharacterBtn;
@synthesize hostRoleView;
@synthesize guestRoleView;
@synthesize hostWinTag;
@synthesize guestWinTag;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    maskView.alpha = 0.0;
    playAgainBtn.alpha = 0.0;
    backToMenuBtn.alpha = 0.0;
    selectCharacterBtn.alpha = 0.0;
    
    for (NSInteger i = 0; i < 5; i++)
    {
        hostSaysViews[i] = [[self view] viewWithTag:i+100];
        guestSaysViews[i] = [[self view] viewWithTag:i+200];
    }
    
    [self setRoleView];
}

- (void)setRoleView
{
    GameDataManager* pManager = [GameDataManager sharedInstance];
    
    NSInteger hostIndex = pManager.hostSelectedRole;
    NSInteger guestIndex = pManager.guestSelectedRole;
    
    NSDictionary* pHostDictionary = [NSDictionary dictionaryWithDictionary:[pManager.roles objectAtIndex:hostIndex]];
    NSDictionary* pGuestDictionary = [NSDictionary dictionaryWithDictionary:[pManager.roles objectAtIndex:guestIndex]];
	
	NSString* winSay = nil; ;
	NSString* loseSay= nil;
    
    if (random()&1)
        winSay = @"win_say_1";
    else
        winSay = @"win_say_2";
    
    if (random()&1)
        loseSay = @"lose_say_1";
    else
		loseSay= @"lose_say_2";
    
    NSArray* hostSays = nil;
    NSArray* guestSays = nil;
    
    if (pManager.whoWin == 1)
    {
        hostRoleView.image = [UIImage imageNamed:[pHostDictionary objectForKey:@"win_pic"]];
        guestRoleView.image = [UIImage imageNamed:[pGuestDictionary objectForKey:@"lose_pic"]];
        hostWinTag.image = [UIImage imageNamed:@"ResultWin.png"];
        guestWinTag.image = [UIImage imageNamed:@"ResultLose.png"];
        
        hostSays = [StringSlicer SliceStr:[pHostDictionary objectForKey:winSay] subLen:20] ;
		guestSays = [StringSlicer SliceStr:[pGuestDictionary objectForKey:loseSay] subLen:20] ;
    }
    else if (pManager.whoWin == 2)
    {
        hostRoleView.image = [UIImage imageNamed:[pHostDictionary objectForKey:@"lose_pic"]];
        guestRoleView.image = [UIImage imageNamed:[pGuestDictionary objectForKey:@"win_pic"]];
        hostWinTag.image = [UIImage imageNamed:@"ResultLose.png"];
        guestWinTag.image = [UIImage imageNamed:@"ResultWin.png"];
        
        hostSays = [StringSlicer SliceStr:[pHostDictionary objectForKey:loseSay] subLen:20] ;
		guestSays = [StringSlicer SliceStr:[pGuestDictionary objectForKey:winSay] subLen:20] ;
    }
    
    int hostDelta = [self getTextDisplayDelta:hostSays.count];
    int guestDelta = [self getTextDisplayDelta:guestSays.count];
    
    FontManager* pFont = [FontManager sharedInstance];
    
    for (int i = 0 ; i < 5 && i < hostSays.count; i ++) {
        [pFont setFontForView:hostSaysViews[i + hostDelta] WithSize:12 WithString:(NSString*)[hostSays objectAtIndex:i]];
    }
    
    for (int j = 0 ; j < 5 && j < guestSays.count; j ++) {
        [pFont setFontForView:guestSaysViews[j + guestDelta] WithSize:12 WithString:(NSString*)[guestSays objectAtIndex:j]];
    }
}

- (void)showHiddenMenu
{
    [UIView animateWithDuration: 0.4  delay: 0
                        options: UIViewAnimationOptionCurveEaseIn
                     animations: ^(){
                         maskView.alpha = 1.0;
                         playAgainBtn.alpha = 1.0;
                         backToMenuBtn.alpha = 1.0;
                         selectCharacterBtn.alpha = 1.0;
                     }
                     completion: nil
     ];
}

- (NSInteger)getTextDisplayDelta:(NSInteger)count
{
	switch (count) 
    {
		case 1:
			return 2 ;
			break;
		case 2:
			return 1;
			break;
		case 3:
			return 1;
			break;
            
		default:
			return 0 ;
			break;
	}
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight
        || interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        return YES;
    
    return NO;
}

#pragma mark - touches

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    UIImageView *touchView = (UIImageView*)[touch view];
    
    if (touchView ==  playAgainBtn || touchView == backToMenuBtn || touchView == selectCharacterBtn)
    {   
        if (touchView.highlighted == NO)
        {
            playAgainBtn.highlighted = NO;
            backToMenuBtn.highlighted = NO;
            selectCharacterBtn.highlighted = NO;
            
            touchView.highlighted = YES;
            [[AudioManager sharedInstance] PlaySFX:@"SelectSFX"];
        }
        else
        {
            [[AudioManager sharedInstance] PlaySFX:@"ConfirmSFX"];
            
            if (touchView == playAgainBtn)
            {
                [self performSegueWithIdentifier:@"SegueToPkViewController" sender:nil];
            }
            else if (touchView == backToMenuBtn)
            {
                [[AudioManager sharedInstance] PlayBGM:@"WelcomeBGM"];
                [self performSegueWithIdentifier:@"SegueToMainViewController" sender:nil];
            }
            else if (touchView == selectCharacterBtn)
            {
                [[AudioManager sharedInstance] PlayBGM:@"WelcomeBGM"];
                [self performSegueWithIdentifier:@"SegueToRoleSelectViewController" sender:nil];
            }
        }
    }
    else
    {
        [self showHiddenMenu];
    }
}

@end
